<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item style="width: 100%" label="税率" prop="tax_rate">
            <el-input-number v-model="form.tax_rate" :min="1" :max="13" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="width: 100%" label="订单金额" prop="trading_price">
            <el-input v-model="form.trading_price" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="contract_remark" style="width: 100%">
            <el-input v-model="form.contract_remark" type="textarea" :rows="4" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { taxRate } from '@/api/project'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      customers: [],
      form: {},
      rules: {
        tax_rate: [{ required: true, trigger: 'blur', message: '税率不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          taxRate(this.form).then(response => {
            this.getData()
            this.handleCancel()
            this.$message.success('编辑成功')
          }).catch(err => {
            this.$message.error(err.msg)
          })
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
